package cn.imu.Utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class CryptoUtil {

    private  CryptoUtil(){

    }

    public static String SHA256(String  str){
        MessageDigest messageDigest;
        String encodeStr="";

        try {
            messageDigest=MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr=byte2Hex(messageDigest.digest());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("getSHA256 is error" + e.getMessage());
        }
        return encodeStr;

    }

  /* public static String MD5(String str){
        String resultStr=MD5.digest(str);
        return resultStr.substring(4,resultStr.length());
   }
*/
    /**
     * 把字节数组转换成16进制字符串
     *
     * @param bArray
     * @return
     */
    public static final String byte2Hex(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }


}
